package com.springapp.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.springapp.jsonmodel.ItemJson;
import com.springapp.jsonmodel.JsonHelper;
import com.springapp.model.Item;
import com.springapp.service.impl.CathegoryServiceImpl;
import com.springapp.service.impl.ItemServiceImpl;
import com.springapp.util.ControllerUtil;

/*
 * 
 * @uthor Sim4ik
 * 
 */

@Controller
@RequestMapping("/")
public class ItemController {

    private final Logger log = LoggerFactory.getLogger(ItemController.class);
    @Autowired
    private ItemServiceImpl isi;
    @Autowired
    private CathegoryServiceImpl csi;

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    public @ResponseBody
    String getAll() {
        List<Item> items = isi.getAll();
        log.info("items arr -" + items);
        List<ItemJson> json = new ArrayList<ItemJson>();

        for (int i = 0; i < items.size(); i++) {
            json.add(ItemJson.toModel(items.get(i)));
        }

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String itemsJson = gson.toJson(json);
        log.info(itemsJson);
        return ControllerUtil.printSuccess(itemsJson);
    }

 //XXX changed method:   
    @RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
    public @ResponseBody
    String getItem(@PathVariable("id") String id) {
        Item item = isi.getById(Integer.valueOf(id));
        if (item == null) {
            return ControllerUtil.printError();
        }
        String json = JsonHelper.fromModel(item);
        return ControllerUtil.printSuccess(json);
    }

    @RequestMapping(value = "/item/{id}", method = RequestMethod.DELETE)
    public @ResponseBody
    String delete(@PathVariable("id") String id) {
        Item item = isi.getById(Integer.valueOf(id));
        if (item == null) {
            return ControllerUtil.printError();
        }
        GsonBuilder builder = new GsonBuilder();
        Gson gson = new Gson();
        gson = builder.create();
        ItemJson itemjson = ItemJson.toModel(item);
        String json = gson.toJson(itemjson);
        isi.delete(Integer.valueOf(id));
        return ControllerUtil.printSuccess(json);
    }

    @RequestMapping(value = "/item", method = RequestMethod.POST,
    consumes = "application/json")
    public @ResponseBody
    String add(@RequestBody String json) {
        Gson gson = new Gson();
        ItemJson ij = gson.fromJson(json, ItemJson.class);
        Item item = new Item();
        item.setCathegory(csi.getById(ij.getCategoryId()));
        item.setDescription(ij.getDescription());
        item.setName(ij.getName());
        item.setPrice(ij.getPrice());
        isi.create(item);
        return ControllerUtil.printSuccess(json);
    }

    @RequestMapping(value = "/item/{id}", method = RequestMethod.PUT,
    consumes = "application/json")
    public @ResponseBody
    String update(@RequestBody String json, @PathVariable("id") int id) {
        log.info(json);
        Gson gson = new Gson();
        ItemJson ij = gson.fromJson(json, ItemJson.class);
        log.info("itemJson -" + ij);
        Item item = isi.getById(id);
        if (item == null) {
            return ControllerUtil.printError();
        }
        item.setCathegory(csi.getById(ij.getCategoryId()));
        item.setDescription(ij.getDescription());
        item.setName(ij.getName());
        item.setPrice(ij.getPrice());
        isi.update(item);
        return ControllerUtil.printSuccess(json);
    }
}
